import os, sys
import pygame
from pygame.locals import *
import math
from numpy import *
import pickle
import helpers
import gradmapper

if not pygame.font: print 'Warning, fonts disabled'
if not pygame.mixer: print 'Warning, sound disabled'

level = 3
# higher = faster = lower resoltion. fastest = 5
maxfps = 40


blocksize = 2**level

def gradient((x,y), map, length):
	x = x / 640.0
	y = y / 480.0
	print "x,y: ", x, y
	values = ndimage.map_coordinates(map, [[x-length, x+length],[y,y]])
	gradx = (values[0] - values[1])
	print "values: ", values[0], values[1]
	return gradx
	


class Pellet(pygame.sprite.Sprite):
    def __init__(self, rect=None):
        pygame.sprite.Sprite.__init__(self) 
        self.image, self.rect = helpers.load_image('pellet.png', None, True)
	self.mass = 1
	self.velocity = (0,0)
        if rect != None:
		self.rect = rect

class PyMain:
	def __init__(self, width=640,height=480):
		pygame.init()
		"""Set the window Size"""
		self.width = width
		self.height = height
		self.wunits = width / blocksize
		self.hunits = height / blocksize
		"""Create the Screen"""
		self.screen = pygame.display.set_mode((self.width, self.height))
		self.pellet_sprites = pygame.sprite.Group()
		self.pellet_sprites.add(Pellet(pygame.Rect(64, 64, 64, 64)))
		self.pellet_sprites.add(Pellet(pygame.Rect(128, 128, 64, 64)))
		self.timer = pygame.time.Clock()
		self.mdown = 0;
		
		self.map = gradmapper.map("map",level)
		self.surfimage = helpers.arraytosurface(self.map.gradmap, blocksize)
		self.font = pygame.font.Font(pygame.font.get_default_font(), 20)
		#self.surfimage = helpers.load_image("map.png")[0]
		print self.surfimage

	def MainLoop(self):
		"""This is the Main Loop of the Game"""
		self.timer.tick()
		thesprite = self.pellet_sprites.sprites()[0]
		(x,y) = thesprite.rect.center
		(vx,vy) = (0,0)
		times = range(100)
		counter = 0
		while 1:
			
			self.pellet_sprites.draw(self.screen)
			pygame.display.flip()
			
			time = self.timer.tick(maxfps) / 1000.0
			if x < 0 or x > self.width:
				vx = 0
				x = self.width / 2
			if y < 0 or y > self.height:
				vy = 0
				y = self.height / 2
			fx = helpers.interpolate(self.map.gradmap, (x / float(blocksize),y/ float(blocksize)), 0)
			fy = helpers.interpolate(self.map.gradmap, (x / float(blocksize),y/ float(blocksize)), 1)
			vx += fx * time
			vy += fy * time
			x += vx * time
			y += vy * time
			thesprite.rect.center = (x,y)
			
			times[counter % 100] = time
			if (counter % 100) == 0:
				fpssurf = self.font.render("fps: " + str(int(1 / (reduce(lambda x, y: x+y, times)) * 100)), True, (255,255,255,255))

			self.screen.fill((0, 0, 0, 255))
			self.screen.blit(self.surfimage,(0,0))
			self.screen.blit(fpssurf,(0,0))
			if pygame.mouse.get_pressed()[1]:
				print gradient(pygame.mouse.get_pos(), self.gradmap, 0.1)
			if pygame.mouse.get_pressed()[0]:
				if mdown == 0:
					thesprite.rect.center = pygame.mouse.get_pos()
					mdown = 1
				else:

					pygame.draw.line(self.screen, (255,0,0,255), thesprite.rect.center, pygame.mouse.get_pos(), 2)
					(vx,vy) = (pygame.mouse.get_pos()[0] - thesprite.rect.center[0],pygame.mouse.get_pos()[1] - thesprite.rect.center[1])
			else:
				mdown = 0

			for event in pygame.event.get():
				if event.type == pygame.QUIT: 
					sys.exit()
					
			counter += 1
	
if __name__ == "__main__":
	MainWindow = PyMain()
	MainWindow.MainLoop()
	
	
